package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.product.assistance.GetProductAssistanceBo;
import com.lframework.xingyun.basedata.bo.product.assistance.QueryProductAssistanceBo;
import com.lframework.xingyun.basedata.entity.ProductAssistance;
import com.lframework.xingyun.basedata.service.product.ProductAssistanceService;
import com.lframework.xingyun.basedata.vo.product.assistance.CreateProductAssistanceVo;
import com.lframework.xingyun.basedata.vo.product.assistance.QueryProductAssistanceVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品辅料表 Controller
 *
 */
@Api(tags = "商品辅料表")
@Validated
@RestController
@RequestMapping("/basedata/product/assistance")
public class ProductAssistanceController extends DefaultBaseController {

    @Autowired
    private ProductAssistanceService productAssistanceService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:product:assistance:query"})
    @GetMapping("/query")
    public InvokeResult<List<QueryProductAssistanceBo>> query(@Valid QueryProductAssistanceVo vo) {

        List<ProductAssistance> datas = productAssistanceService.query(vo);

        List<QueryProductAssistanceBo> results = new ArrayList<>();

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryProductAssistanceBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(results);
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:product:assistance:query"})
    @GetMapping
    public InvokeResult<GetProductAssistanceBo> get(@NotBlank(message = "id不能为空！") String id) {

        ProductAssistance data = productAssistanceService.findById(id);
        if (data == null) {
            throw new DefaultClientException("商品辅料表不存在！");
        }

        GetProductAssistanceBo result = new GetProductAssistanceBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:product:assistance:add"})
    @PostMapping
    public InvokeResult<Void> create(@Valid @RequestBody CreateProductAssistanceVo vo) {

        productAssistanceService.create(vo);

        return InvokeResultBuilder.success();
    }
}

